package com.back.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.back.business.ISchoolPhotoService;
import com.back.business.SchoolPhotoServiceImpl;
import com.base.web.BaseAction;
import com.domain.ActivityTable;
import com.domain.SchoolPhoto;
import com.util.comm.Contants;
import com.util.comm.RequestUtil;
import com.util.file.FileUtil;
import com.util.file.ImageSizer;
import com.util.file.XmlUtil;

public class SchoolPhotoAction  extends BaseAction{
	private  SchoolPhoto photo;
	private  File   image;
	private  String  imageFileName;
    private ISchoolPhotoService   photoService;
    private Logger LOGGER = Logger.getLogger(SchoolPhotoAction.class);
    private int total;
	@SuppressWarnings("unchecked")
	private List rows;
    public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List getRows() {
		return rows;
	}
	public void setRows(List rows) {
		this.rows = rows;
	}
	public SchoolPhotoAction (){
    	 photoService =  new SchoolPhotoServiceImpl();  	
    }
    public  String  saveOrUpdateSchool(){
    	if ( photo == null)
    	{
    		photo = new SchoolPhoto();	
    	}
    	if( null == photo.getId() || 0== photo.getId())
    	{
    		photo.setCreatTime(new Date());
    	}
    	if (image != null){
    		String basePath = request.getSession().getServletContext().getRealPath(
			"/");
			String imagePatn = XmlUtil.getValue("imgPath", "sysConfig") + "/";
			String compressPath = XmlUtil.getValue("compression_diagram",
					"sysConfig")
					+ "/";
			String fileName = new Date().getTime() + imageFileName.substring(imageFileName.lastIndexOf("."));
			if (FileUtil.doUpFile(basePath + imagePatn + fileName, image)) {
				ImageSizer.doCompress(image, basePath + compressPath + fileName,
						380, 100, 1.0f, true);
				photo.setPhoto(imagePatn + fileName);
				photo.setPhotoCompress(compressPath + fileName);
			}
    	}
    	try{
    	   boolean  isBoolean =	photoService.saveOrUpdateSchoolPhoto(photo);
    	   if(isBoolean){
    		   return "success";
    	   }
    	}catch (Exception e) {
    		LOGGER.error(" SchoolPhotoAction saveOrUpdate error ");
    	    return "error";
		}
    	 return "error";  		
    }
	public String search() {
		String type = RequestUtil.getParam(request, "type", "");
		String page = RequestUtil.getParam(request, "page", "");
		String row = RequestUtil.getParam(request, "rows", "");
		Map<String, String> parameMap = new HashMap<String, String>();
		parameMap.put("type", type);
		parameMap.put("page", page);
		parameMap.put("row", row);
		List<SchoolPhoto> aclist = photoService.findByParameMap(parameMap);
		int count = photoService.count(parameMap);
		if (aclist != null) {
			total = count;
			rows = aclist;
		}
		return Contants.JSONSTRING_RESULT_NAME;

	}
	public String deleteById() {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String idStr = RequestUtil.getParam(request, "ids", "");
			boolean isboolean = photoService.deleteById(idStr);
			out.println("{result:" + isboolean + "}");
		} catch (IOException e) {
			e.printStackTrace();
			LOGGER.error("SchoolPhotoAction  deleteIds  error ", e);
		}

		return null;
	}
	public String findSchoolByTId() {
		String id = RequestUtil.getParam(request, "id", "");
		SchoolPhoto actObj = photoService.findById(id);
		request.setAttribute("photo", actObj);
		return "modif";
	}

	public SchoolPhoto getPhoto() {
		return photo;
	}
	public void setPhoto(SchoolPhoto photo) {
		this.photo = photo;
	}
	public File getImage() {
		return image;
	}
	public void setImage(File image) {
		this.image = image;
	}
	public String getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
}
